using UnityEngine;
using System.Collections;

public class ScreenManager : MonoBehaviour 
{
	public static ScreenManager Instance;
	
	public GameObject ResultScreen;
	
	public UILabel bestTime;
	public UILabel currentTime;
	
	public UISprite resultBackground;
	
	public UISprite[] Stars;
	
	int starScore = 0;
	
	void Awake ()
	{
		Instance = this;
		Time.timeScale = 1.0F;
	}
	
	void Start () 
	{
		ResultScreen.SetActive(false);
	}
	
	public void CallResultScreen ()
	{
		if ( ResultScreen.activeSelf == false )
		{			
			ResultScreen.SetActive(true);
			CheckScore ();
			Time.timeScale = 0.0F;
		}
	}
	
	void CheckScore ()
	{	
		if ( LevelManager.Instance.currentTime >= LevelManager.Instance.endTime )
		{
			starScore = 0;
		}
		else if ( LevelManager.Instance.currentTime <= LevelManager.Instance.defaultTime )
		{
			starScore = 3;
		}
		else if ( LevelManager.Instance.currentTime <= ((( LevelManager.Instance.endTime - LevelManager.Instance.defaultTime ) * 0.5  ) + LevelManager.Instance.defaultTime )
				&& LevelManager.Instance.currentTime > LevelManager.Instance.defaultTime )
		{
			starScore = 2;
		}
		else if ( LevelManager.Instance.currentTime < LevelManager.Instance.endTime
				&& LevelManager.Instance.currentTime > ((( LevelManager.Instance.endTime - LevelManager.Instance.defaultTime ) * 0.5 ) + LevelManager.Instance.defaultTime ))
		{
			starScore = 1;
		}
	
		
		 ShowResults ( starScore );
		
	}
	
	void ShowResults ( int scores )
	{
		switch ( starScore )
		{
		case 0:
				resultBackground.spriteName = "BG_TryAgain";
				
				for ( int i = 0; i < 3; i++ )
					Stars[i].spriteName = "Star2";
			
			Debug.Log("TRY AGAIN");
				
			break;
			
		case 1:
				resultBackground.spriteName = "BG_Excellent";
				
				Stars[0].spriteName = "Star1";
			
				for ( int i = 1; i < 3; i++ )
					Stars[i].spriteName = "Star2";
			
			Debug.Log("1 star");
			
			break;
			
		case 2:
				resultBackground.spriteName = "BG_Excellent";
				
				for ( int i = 0; i < 2; i++ )
					Stars[i].spriteName = "Star1";
			
				Stars[2].spriteName = "Star2";
			
			Debug.Log("2 stars");
			
			break;
			
		case 3:
				resultBackground.spriteName = "BG_Excellent";
				
				for ( int i = 0; i < 3; i++ )
					Stars[i].spriteName = "Star1";
			
			Debug.Log("3 stars");
			
			break;
		}
		
		if ( PlayerPrefs.GetInt("_bestTime") > (int)LevelManager.Instance.currentTime )
			PlayerPrefs.SetInt("_bestTime", (int)LevelManager.Instance.currentTime);
		
		bestTime.text = PlayerPrefs.GetInt("_bestTime").ToString() + " sec.";
		currentTime.text = (LevelManager.Instance.currentTime).ToString() + " sec.";
	}
	
	void CallRestart ()
	{
		Application.LoadLevel("GenericLevel");
	}
	
	void CallMainMenu ()
	{
		Application.LoadLevel("Game");
	}
	
	void CallNext ()
	{
	}
}
